package com.example.mtblog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mtblog.common.Result;
import com.example.mtblog.common.common;
import com.example.mtblog.dto.ArticleInfo;
import com.example.mtblog.dto.UserInfo;
import com.example.mtblog.entity.Comment;
import com.example.mtblog.mapper.CommentMapper;
import com.example.mtblog.tool.RedisUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/***
 * @description: 修复Redis与数据库数据不一致的问题
 * @author MT
 * @date 2024/4/22 22:10
 */
@Slf4j
@Component
public class RepairController {

    @Resource
    CommentMapper commentMapper;

    /***
    * @description: 修复评论
    * @author MT
    * @date 2024/4/22 22:11
    */
    public  Result<?> repairComment(){
        try {
            List<String> keys = RedisUtils.scanKeysSimple(common.articleInfoToken);
            for (String key:keys){
                ArticleInfo articleInfo = RedisUtils.getCacheObject(key);
                QueryWrapper<Comment> wrapper = new QueryWrapper<>();
                wrapper.eq("article_id",articleInfo.getId());
                long count = commentMapper.selectCount(wrapper);
                articleInfo.setReadNum((int) count);
                common.setArticleInfo(articleInfo);
            }

        }catch (Exception e){
            String msg = "error in repair/repairComment " + "\n"  +e ;
            log.error(msg);
            return Result.error(msg);
        }
        return Result.success();
    }


}


